package exam.controller;
import exam.bean.FinanceInfo;
import exam.service.IFinanceInfoService;
import exam.utils.FileUtil;
import exam.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Map;

@RestController()
@RequestMapping("/financeInfos")
public class FinanceInfoController {
    @Autowired
    IFinanceInfoService financeInfoService;

    @Value("${upload.uploadPath}")
    private String uploadPath;

//    @PreAuthorize("hasAuthority('sys:finance:list')")
    @ResponseBody
    @PostMapping("/searchInfo/{pageNum}/{pageSize}")
    public Msg searchInfo(@PathVariable String pageNum, @PathVariable String pageSize, @RequestBody Map queryMap){
        return Msg.success().add("pageInfo",financeInfoService.selectInfo(Integer.parseInt(pageNum),
                Integer.parseInt(pageSize),queryMap));
    }


    @ResponseBody
//    @PreAuthorize("hasAuthority('sys:sysParam:companyInfo:add')")
    @PostMapping("/add")
    public Msg insertInfo(@RequestPart("financeInfo") FinanceInfo financeInfo,
                          @RequestPart("files") MultipartFile[] file){
        financeInfo.setUtilFiles(Arrays.asList(file));
        boolean re = financeInfoService.insertInfo(financeInfo,uploadPath) > 0;
        return re ? Msg.success().setMessage("数据添加成功") :Msg.fail().setMessage("数据添加失败");
    }

    @ResponseBody
    @GetMapping("/detail")
    public Msg detail(@RequestParam(value="id") int id){
        return Msg.success().add("financeInfo",financeInfoService.selectInfoSingle(id));
    }

    @ResponseBody
//    @PreAuthorize("hasAuthority('sys:sysParam:companyInfo:delete')")
    @GetMapping("/delete")
    public Msg delete (@RequestParam(value="id") int id){
        boolean re = financeInfoService.deleteInfo(id) > 0 ;
        return Msg.success().setMessage(re ? "删除成功":"删除失败");
    }

    @ResponseBody
//    @PreAuthorize("hasAuthority('sys:sysParam:companyInfo:update')")
    @PostMapping("/upData")
    public Msg update(@RequestPart("financeInfo") FinanceInfo financeInfo,
                      @RequestPart(value = "files",required = false) MultipartFile[] files){
        financeInfo.setUtilFiles(Arrays.asList(files));
        boolean re = financeInfoService.updateInfo(financeInfo,uploadPath) > 0;
        return re ? Msg.success().setMessage("修改成功"):Msg.fail().setMessage("修改失败");
    }


    @GetMapping("/downLoad")
    public void downLoad(HttpServletResponse response, String fileName, String fileNameSrc){
        FileUtil.downLoadFile(response,uploadPath,fileName,fileNameSrc);
    }
}
